// Copyright (C) 2023 即时通讯网(52im.net) & Jack Jiang.
// The RainbowChat Project. All rights reserved.
// 
// 【本产品为著作权产品，合法授权后请放心使用，禁止外传！】
// 【本次授权给：<北京小羊驼科技有限公司>，授权编号：<NT231212144350>，代码指纹：<A.702363430.550>，技术对接人微信：<ID: wxid_wbyootvkdcgj22>】
// 【授权寄送：<收件：苏先生、地址：北京市大兴区北京密码西区6号楼1单元301、电话：18613885610、邮箱：bd@huiyuanxiang-inc.com>】
// 
// 【本系列产品在国家版权局的著作权登记信息如下】：
// 1）国家版权局登记名(简称)和权证号：RainbowChat    （证书号：软著登字第1220494号、登记号：2016SR041877）
// 2）国家版权局登记名(简称)和权证号：RainbowChat-Web（证书号：软著登字第3743440号、登记号：2019SR0322683）
// 3）国家版权局登记名(简称)和权证号：RainbowAV      （证书号：软著登字第2262004号、登记号：2017SR676720）
// 4）国家版权局登记名(简称)和权证号：MobileIMSDK-Web（证书号：软著登字第2262073号、登记号：2017SR676789）
// 5）国家版权局登记名(简称)和权证号：MobileIMSDK    （证书号：软著登字第1220581号、登记号：2016SR041964）
// * 著作权所有人：江顺/苏州网际时代信息科技有限公司
// 
// 【违法或违规使用投诉和举报方式】：
// 联系邮件：jack.jiang@52im.net
// 联系微信：hellojackjiang
// 联系QQ号：413980957
// 授权说明：http://www.52im.net/thread-1115-1-1.html
// 官方社区：http://www.52im.net
package com.x52im.rbchat.logic.chat_root.sendlocation.impl;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.eva.epc.common.util.CommonUtils;
import com.x52im.rbchat.R;
import com.x52im.rbchat.logic.chat_root.model.LocationMeta;
import com.x52im.rbchat.logic.chat_root.sendlocation.utils.LocationOnItemClickLisenter;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * 地址的适配器.
 * 本类的原始代码来自开源工程：https://github.com/xiaofuchen/WeixinLocation
 *
 * @author 原作者：XiaoFu, 优化和修订：JackJiang
 * @since 6.0
 */
public class GetLocationPOIListAdapter extends RecyclerView.Adapter<GetLocationPOIListAdapter.MyHolder>
{
    private final static String TAG = GetLocationPOIListAdapter.class.getSimpleName();

    private Context mContext;
    private List<PoiItem> mList;
    private int selectPosition = -1;
    private LocationOnItemClickLisenter mOnItemClickLisenter;

    public GetLocationPOIListAdapter(Context context, List<PoiItem> list) {
        this.mContext = context;
        this.mList = list;
    }

    public void notifyDataSetChangedAndResetSelection()
    {
//        selectPosition = -1;
        setSelectPosition(-1);
        super.notifyDataSetChanged();
    }

//    public void setList(List<PoiItem> list) {
//        this.mList = list;
//        selectPosition = -1;
//        super.notifyDataSetChanged();
//    }

    protected void afterSetSelectPosition() {}

    public void setSelectPosition(int position) {
        this.selectPosition = position;
        super.notifyDataSetChanged();
        afterSetSelectPosition();
    }

    public int getSelectPositon(){
        return selectPosition;
    }

    public void setOnItemClickLisenter(LocationOnItemClickLisenter onItemClickLisenter) {
        this.mOnItemClickLisenter = onItemClickLisenter;
    }

    @Override
    public GetLocationPOIListAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyHolder myHolder = new MyHolder(LayoutInflater.from(mContext).inflate(R.layout.chatting_get_location_pio_item, parent, false));
        return myHolder;
    }

    public LocationMeta getSelectedItem()
    {
        LocationMeta lm = null;

        if (null != mList && 0 < mList.size()) {
            int position = getSelectPositon();
            if (position < 0) {
                position = 0;
            } else if (position > mList.size()) {
                position = mList.size();
            }
            PoiItem poiItem = mList.get(position);


            lm = new LocationMeta();
            lm.setLocationTitle(poiItem.getTitle());
            lm.setLatitude(poiItem.getLatLonPoint().getLatitude());
            lm.setLongitude(poiItem.getLatLonPoint().getLongitude());

            if(poiItem instanceof GetLocationPOIEntity) {
                lm.setLocationContent(((GetLocationPOIEntity) poiItem).getContentForShow());
            }
            else {
                lm.setLocationContent(poiItem.getProvinceName()
                        + poiItem.getCityName() + poiItem.getAdName() + poiItem.getSnippet());
            }
        }

        return lm;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        holder.itemView.setTag(position);
        PoiItem poiItem = mList.get(position);
        if (position == getSelectPositon()) {
            holder.mCheckBox.setChecked(true);
        } else {
            holder.mCheckBox.setChecked(false);
        }
        holder.mTvTitle.setText(poiItem.getTitle());

//        Log.d(TAG, "!!! getProvinceName="+poiItem.getProvinceName()+", getCityName="
//                +poiItem.getCityName()+", getAdName="+poiItem.getAdName()+", getSnippet="+poiItem.getSnippet());
//        Log.d(TAG, "!!! poiItem.toString="+poiItem.toString());

        String content;
        if(poiItem instanceof GetLocationPOIEntity)
            content = ((GetLocationPOIEntity) poiItem).getContentForShow();
        else
            content = (poiItem.getProvinceName() + poiItem.getCityName() + poiItem.getAdName() + poiItem.getSnippet());

        if(CommonUtils.isStringEmpty(content, true))
            content = "经度："+poiItem.getLatLonPoint().getLongitude()+" 纬度："+poiItem.getLatLonPoint().getLatitude();
        holder.mTvMessage.setText(content);

        holder.itemView.setOnClickListener(view -> {
            int position1 = (Integer) view.getTag();
            setSelectPosition(position1);
            if (null != mOnItemClickLisenter) {
                mOnItemClickLisenter.onItemClick(position1);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder {
        TextView mTvTitle;
        TextView mTvMessage;
        CheckBox mCheckBox;

        public MyHolder(View itemView) {
            super(itemView);
            mTvTitle = (TextView) itemView.findViewById(R.id.chatting_get_location_pio_item_tv_title);
            mTvMessage = (TextView) itemView.findViewById(R.id.chatting_get_location_pio_item_tv_message);
            mCheckBox = (CheckBox) itemView.findViewById(R.id.chatting_get_location_pio_item_checkBox);
        }
    }

}
